package com.glory.brepda.activity.tool;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;

public class DispensingheadFixtureWashActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etToolID)
    FreeUI_ClearEditText etToolID;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_dispensing_head_fixture_wash;
    }

    @Override
    protected void initView() {
        tvTitle.setText("点胶头清洗");
        tvBaseSubmit.setVisibility(View.GONE);
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {
        // 编辑组件输入框时触发的事件，设置这些事件的原因是移除了提交按钮，让页面所有信息都输入之后能够自动提交信息
        initEdit(etToolID,s->{ DispensingHeadWash(s, "TOOLID"); });
        initEdit(etEqpID,s->{ DispensingHeadWash(s, "EQUIPMENTID"); });
    }

    private void DispensingHeadWash(String text, String type) {
        JsonObject map = null;
        boolean checkFlag = false;

        if (StringUtils.equals("TOOLID", type) && StringUtils.isNotEmpty(etEqpID.getText())) {

            map = new JsonObject();
            map.addProperty("EQUIPMENTID", String.valueOf(etEqpID.getText()));
            checkFlag = true;

        } else if (StringUtils.equals("EQUIPMENTID", type) && StringUtils.isNotEmpty(etToolID.getText())) {

            map = new JsonObject();
            map.addProperty("TOOLID", String.valueOf(etToolID.getText()));
            checkFlag = true;

        }
        if (!checkFlag) {
            // 检测到还有一些输入框没有输入信息，进行切换焦点
            if (StringUtils.equals("TOOLID", type)) {
                // 输入的信息是治具的信息，则切换焦点至 设备输入框
                etEqpID.requestFocus();
            } else {
                // 反之，切换焦点至 治具输入框
                etToolID.requestFocus();
            }
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                (new TypeToken<BaseResponseBean<Object>>() {
                }.getType(),getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<Object>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(DispensingheadFixtureWashActivity.this, false, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                    etToolID.setText("");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<Object>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "TOOLCLEAR");
            }
        };
        map.addProperty("ACTIONTYPE", "DISPENSINGHEADCLEAR");
        map.addProperty(type, text);
        NetUtils.requestPostNet("IF.TOOLCLEAR", "Common", DispensingheadFixtureWashActivity.this, map, callBack);
    }

}